import { PicUrl } from "../../constans/index";
import { CounterApi } from "../../api/counter.api";
interface CounterItem {
  id: number;
  project_name: string;
  totalCount: number;
}

interface IData {
  counterList: CounterItem[];
  loading: boolean;
  picUrl: string;
}

interface ICustom {
  loadCounterList: () => void;
  onAddCounter: () => void;
  onCounterTap: (e: WechatMiniprogram.TouchEvent) => void;
}

Page({
  data: {
    counterList: [] as CounterItem[],
    loading: false,
    picUrl: PicUrl,
    buttons: [
      {
        text: "取消",
      },
      {
        text: "确定",
      },
    ],
    createCounterDailogShow: false,
    newConterName: "",
  },

  onLoad() {
    this.loadCounterList();
  },

  // 加载计数器列表
  async loadCounterList() {
    this.setData({ loading: true });

    let res = await CounterApi.getProjects();
    this.setData({ loading: false });
    console.log(res.data);

    if (res.success) {
      this.setData({
        counterList: res.data,
      });
    }
  },

  // 跳转到新建计数器页面
  onAddCounter() {
    // wx.navigateTo({
    //   url: '/pages/counter/counter'
    // })
    this.setData({
      createCounterDailogShow: true,
    });
  },

  // 跳转到计数器详情页
  onCounterTap(e: WechatMiniprogram.TouchEvent) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/counter/counter?id=${id}`,
    });
  },

  async tapDialogButton({ detail: { index } }: any) {
    if (index === 0) {
      this.setData({
        createCounterDailogShow: false,
      });
    } else if (index === 1) {
      if (this.data.newConterName.length < 1) {
        wx.showToast({
          title: "请输入名称", // 提示内容
          icon: "error", // 图标，可选值：'success'、'loading'、'none'
          duration: 1000, // 提示持续时间，单位毫秒
        });
      } else if (this.data.newConterName.length > 20) {
        wx.showToast({
          title: "字数超限(20)", // 提示内容
          icon: "error", // 图标，可选值：'success'、'loading'、'none'
          duration: 1000, // 提示持续时间，单位毫秒
        });
      } else {
        let res = await CounterApi.createProject(this.data.newConterName);
        if (res.success) {
          wx.showToast({
            title: "创建成功",
            icon: "success",
          });
          this.loadCounterList();
          this.setData({
            createCounterDailogShow: false,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error",
          });
        }
      }
    }
  },
  goBack() {
    wx.navigateBack();
  },

  async onDeleteItem(item: any) {
    console.log(item.target.dataset.id);
    let confirm = await wx.showModal({
      title: "确认删除",
      content: "是否确认删除该计数器?",
      confirmText: "删除",
      cancelText: "取消",
    });

    if (!confirm.confirm) {
      return;
    }
    // 处理删除逻辑
    let res = await CounterApi.deleteProject(item.target.dataset.id);
    if (res.success) {
      wx.showToast({
        title: "删除成功",
        icon: "success",
      });
      this.loadCounterList();
    }
  },
});
